﻿using System;
using System.Collections.Generic;

namespace RSSReader.classesCustom
{
    public class UserDataHandler
    {
        private DataHandler appDataHandler;
        private Int32 userID;             
        //changed to test if Dictionary or List is more convenient to do the job
        private Dictionary<Int32,Site> userCustomSiteList;
        private int recordcountSQL;
        
        public UserDataHandler()
        {
            userID = 1; //user anonymous  - without authentication            
            this.appDataHandler = (DataHandler)System.Web.HttpContext.Current.Application["dataHandler"];
            
            List<Int32> visibleSiteIDs = this.appDataHandler.getUserVisibleSiteIndexes(userID);
            userCustomSiteList =new Dictionary<Int32,Site>();
            foreach (Site s in appDataHandler.webSites)
            {
                Site ns = new Site(s);
                userCustomSiteList.Add(ns.ID, ns);
                foreach (Int32 visibleSiteID in visibleSiteIDs)
                    if (visibleSiteID == ns.ID)                    
                        userCustomSiteList[ns.ID].visible =true;                    
            }            
        }

        public List<Site> getUserCustomSites()
        {
            //TODO: may return array ????
            Site[] sa=new Site[userCustomSiteList.Count];
            userCustomSiteList.Values.CopyTo(sa,0);
            List<Site> ls=new List<Site>(sa);
            return ls;
        }

        /// <summary>
        /// enabels/disable viewing articles from a certain site
        /// specified as parameters
        /// </summary>
        /// <param name="siteID"> Id of the site from which the articles should be showed or not</param>
        /// <param name="visibility">specifies if articles should be showed or not </param>
        public void setSiteVisibility(Int32 siteID, bool visibility)
        {
            //save in memory
            userCustomSiteList[siteID].visible = visibility;
            //save to database
            appDataHandler.setSiteVisibilityforUser(userID,siteID,visibility);
        }

        /// <summary>
        /// returns paged result for online article query
        /// customized to user's and objectdatasource custom paging need
        /// </summary>
        /// <returns>
        /// List with Article-s (one page) available online
        /// </returns>
        public List<Article> getUserCustomXMLArticles(int startRowIndex, int maximumRows)
        {
            List<Article> pagedArticleList = new List<Article>();
            
            int page=0;
            for (int i = 0; i < appDataHandler.onlineArticles.Count; i++)
                foreach (Int32 id in userCustomSiteList.Keys)
                    if ((appDataHandler.onlineArticles[i].siteID == id) 
                        & (userCustomSiteList[id].visible))
                    {
                        page++;
                        if ((page > startRowIndex) & (page <= startRowIndex + maximumRows))
                            pagedArticleList.Add(appDataHandler.onlineArticles[i]);
                        break; // do no more iterations if found
                    }            
            return pagedArticleList;
        }

        /// <summary>
        /// needed for objectdatasource custom paging
        /// </summary>
        /// <returns>
        /// 'int' containing total number of articles
        /// </returns>
        public Int32 getUserCustomXMLArticlesCount()
        {
            Int32 articleCountXML=0;
            foreach (Article a in appDataHandler.onlineArticles)
                if (userCustomSiteList[a.siteID].visible)
                    articleCountXML++;

            return articleCountXML;
        }
        
        /// <summary>
        /// needed for objectdatasource detail view
        /// </summary>
        /// <returns>
        /// 'int' containing total number of articles
        /// </returns>
        public Article getUserCustomXMLArticleById(Int32 id)
        {
            Article art = null;

            foreach (Article a in appDataHandler.onlineArticles)
                if (a.ID == id)
                {
                    art = a;
                    break;
                }
            return art;
        }
        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">
        /// article id from xml list
        /// not the same with id from database !!!
        /// </param>        
        public void saveUserCustomXMLArticle (Int32 articleID)
        {            
            try
            {
                appDataHandler.saveXMLArticleToDatabase(articleID, userID);                
            }
            catch (Exception )
            {
                //TODO: log-  error unable to save article
                Console.WriteLine("bullshit");
            }            
        }



        /// <summary>
        /// returns paged result for saved articles 
        /// customized to user's and objectdatasource custom paging need
        /// </summary>
        /// <returns>
        /// List with Article-s (one page) saved to database
        /// </returns>
        public List<Article> getUserCustomSQLArticles(int startRowIndex, int maximumRows) 
        {
            KeyValuePair<Int32, List<Article>> result= appDataHandler.getUserCustomSQLArticles(startRowIndex,maximumRows,userID);
            recordcountSQL= result.Key;
            return result.Value;
        }

        /// <summary>
        /// needed for objectdatasource custom paging
        /// </summary>
        /// <returns>
        /// 'int' containing total number of articles
        /// </returns>
        public int getUserCustomSQLArticlesCount()
        {
            return recordcountSQL;
        }

        public Article getUserCustomSQLArticleById(Int32 id)
        {
            return appDataHandler.getSQLArticlebyId(id);
        }

        public void deleteUserCustomSQLArticle(Int32 articleID)
        {
            appDataHandler.deleteSQLArticleforUser(articleID, userID);
        }    
    }
}